package com.hackcloud.shardingjdbc.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hackcloud.shardingjdbc.dao.center.ApplyCenterContactRepository;
import com.hackcloud.shardingjdbc.dao.center.ApplyCenterInfoRepository;
import com.hackcloud.shardingjdbc.dao.center.ApplyCenterMainRepository;
import com.hackcloud.shardingjdbc.domain.center.ApplyCenterContact;
import com.hackcloud.shardingjdbc.domain.center.ApplyCenterInfo;
import com.hackcloud.shardingjdbc.domain.center.ApplyCenterMain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
public abstract class AbstractApplyCenterService {
    public static final String SERVICE_SUFFIX = "DATA_CLEAN";
    @Autowired
    ApplyCenterMainRepository applyCenterMainRepository;
    @Autowired
    ApplyCenterContactRepository applyCenterContactRepository;
    @Autowired
    ApplyCenterInfoRepository applyCenterInfoRepository;
    @Autowired
    IdWorker idWorker;

    @Transactional(value = "shardingTransactionManager", rollbackFor = Exception.class)
    public void save(String json) {
        try {
            JSONObject jsonObject = JSON.parseObject(json);
            ApplyCenterMain applyCenterMain = jsonObject.toJavaObject(ApplyCenterMain.class);
            this.saveApplyCenterMain(applyCenterMain);
            this.saveApplyCenterInfo(json, applyCenterMain);
            this.saveApplyCenterContactList(applyCenterMain);
        } catch (Throwable e) {
            log.error("失败", e);
        }
    }

    public List<ApplyCenterMain> read() {
        return applyCenterMainRepository.findAll();
    }

    public void saveApplyCenterInfo(String json, ApplyCenterMain applyCenterMain) {
        ApplyCenterInfo applyCenterInfo = new ApplyCenterInfo();
        applyCenterInfo.setServiceFlowNo(applyCenterMain.getCertNo());
        applyCenterInfo.setRequest(json);
        applyCenterInfo.setClientFlowNo(applyCenterMain.getClientFlowNo());
        applyCenterInfo.setScene(applyCenterMain.getScene());
        applyCenterInfoRepository.saveAndFlush(applyCenterInfo);
    }

    /**
     * 保存主表
     *
     * @param applyCenterMain
     */
    public void saveApplyCenterMain(ApplyCenterMain applyCenterMain) {
        final long serialNo = idWorker.nextId();
        String serviceFlowNo = getServiceFlowNo(applyCenterMain.getCertNo());
        //sceneCenter.setId(serialNo);
        applyCenterMain.setServiceFlowNo(serviceFlowNo);
        applyCenterMainRepository.saveAndFlush(applyCenterMain);
    }

    public void saveApplyCenterContactList(ApplyCenterMain applyCenterMain) {
        final List<ApplyCenterContact> contactList = applyCenterMain.getContactList();
        if (CollectionUtils.isEmpty(contactList)) return;
        for (ApplyCenterContact sceneCenterContactList : contactList) {
            //sceneCenterContactList.setId(idWorker.nextId());
            sceneCenterContactList.setServiceFlowNo(applyCenterMain.getServiceFlowNo());
            applyCenterContactRepository.save(sceneCenterContactList);
        }
    }

    private String getServiceFlowNo(String certNo) {
        final long serialNo = idWorker.nextId();
        String hashCode = String.valueOf(certNo.hashCode());
        String suffix = hashCode.substring(hashCode.length() - 3);
        String serviceFlowNo = serialNo + suffix;
        log.warn("serviceFlowNo:{}", serviceFlowNo);
        return serviceFlowNo;
    }
}
